package com.tiled.view;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.Stroke;

import javax.swing.JPanel;
import javax.swing.Scrollable;
import javax.swing.SwingConstants;

import com.tiled.core.Map;
import com.tiled.core.MapLayer;
import com.tiled.core.TileLayer;

class MapView extends JPanel implements Scrollable
{
	private static final long serialVersionUID = 1L;
	private Map map;
    private MapRenderer renderer;

    public MapView(Map map) {
        this.map = map;
        renderer = createRenderer(map);

        setPreferredSize(renderer.getMapSize());
        setOpaque(true);
    }

    public void paintComponent(Graphics g) {
        final Graphics2D g2d = (Graphics2D) g.create();
        final Rectangle clip = g2d.getClipBounds();

        // Draw a gray background
        g2d.setPaint(new Color(100, 100, 100));
        g2d.fill(clip);
        
        // Draw grid line
        grawGrid(g2d);

        // Draw each tile map layer
        for (MapLayer layer : map) {
            if (layer instanceof TileLayer) {
                renderer.paintTileLayer(g2d, (TileLayer) layer);
            }
        }
    }

    private void grawGrid(Graphics2D g2d) {
    	Stroke dash = new BasicStroke(1f,BasicStroke.CAP_BUTT,BasicStroke.JOIN_MITER,1f,new float[]{2,2,},0f);
    	g2d.setStroke(dash);
    	g2d.setColor(Color.black);
    	
    	for(int i=0;i<map.getHeight();i++){//横线，y变动
    		g2d.drawLine(0, i*map.getTileHeight(), map.getWidth()*map.getTileWidth(), i*map.getTileHeight());
    	}
    	for(int j=0;j<map.getWidth();j++){//竖线，x变动
    		g2d.drawLine(j*map.getTileWidth(), 0, j*map.getTileWidth(), map.getHeight()*map.getTileHeight());
    	}
	}

	private static MapRenderer createRenderer(Map map) {
        switch (map.getOrientation()) {
            case Map.ORIENTATION_ORTHOGONAL:
                return new OrthogonalRenderer(map);
            default:
                return null;
        }
    }

    public Dimension getPreferredScrollableViewportSize() {
        return getPreferredSize();
    }

    public int getScrollableUnitIncrement(Rectangle visibleRect,
                                          int orientation, int direction) {
        if (orientation == SwingConstants.HORIZONTAL)
            return map.getTileWidth();
        else
            return map.getTileHeight();
    }

    public int getScrollableBlockIncrement(Rectangle visibleRect,
                                           int orientation, int direction) {
        if (orientation == SwingConstants.HORIZONTAL) {
            final int tileWidth = map.getTileWidth();
            return (visibleRect.width / tileWidth - 1) * tileWidth;
        } else {
            final int tileHeight = map.getTileHeight();
            return (visibleRect.height / tileHeight - 1) * tileHeight;
        }
    }

    public boolean getScrollableTracksViewportWidth() {
        return false;
    }

    public boolean getScrollableTracksViewportHeight() {
        return false;
    }
}